#include <iostream>
using std::cout;
using std::endl;

//数组指针
void test0(){
    int arr[5] = {1,2,3,4,5};

    //arr代表首个元素的首地址
    cout << arr << endl;
    cout << arr + 1 << endl;

    //&arr代表数组的首地址
    cout << &arr << endl;
    cout << &arr + 1 << endl;

    int (*p)[5] = &arr;
    cout << p + 1 << endl;

    cout << endl;
    for(int i = 0; i < 5; ++i){
        cout << (*p)[i] << endl;
    }
}

//指针数组
//存放元素为指针的数组
void test1(){
    int num1 = 1, num2 = 2, num3 = 3;
    int * p1 = &num1;
    int * p2 = &num2;
    int * p3 = &num3;
    int * arr[3] = {p1,p2,p3};
    for(int i = 0; i < 3; ++i){
        cout << *arr[i] << endl;
    }
}

void test2(){
    int num1 = 1, num2 = 2, num3 = 3;
    int arr[3] = {num1,num2,num3};
    cout << &num1 << endl;
    cout << arr << endl;

}

//函数指针
//指向函数的指针，可以利用这种指针调用函数
void print(){
    cout << "print()" << endl;
}

int func(int x,int y){
    return x + y;
}

void test3(){
    //定义函数指针需要确定其指向的函数的
    //返回类型和参数信息
    void (*p)() = print;
    p();
    
    int (*p2)(int,int) = func;
    cout << p2(3,6) << endl;

    //完整写法
    void (*p3)() = &print;
    (*p3)();
}

//指针函数
//返回值是指针的函数
//要确保返回的指针所指向的内容生命周期比函数更长
int num = 100;
int * func2(){
    return &num;
}

void test4(){
    cout << *func2() << endl;
}

int main(void){
    test0();
    return 0;
}
